package ar.edu.unq.wannaplay.persistence.daos;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import ar.edu.unq.wannaplay.employee.Employee;
import ar.edu.unq.wannaplay.persistence.genericdaos.HibernateGenericDao;
import ar.edu.unq.wannaplay.utils.LicenceCalendar;

public class EmployeeDAO extends HibernateGenericDao<Employee> {
    private static final long serialVersionUID = 1L;

    @Override
    protected Class<Employee> getDomainClass() {
        return Employee.class;
    }

    @Override
    public void delete(final Employee employee) {
        Employee emp = this.findEmployeeByDNI(employee.getData().getId());
        super.delete(emp);
    }

    public LicenceCalendar getHolidaysCalendar(final int month, final int year) {
        List<Employee> employees = this.findAll();
        return new LicenceCalendar(employees, month, year);
    }

    public Employee findEmployeeByDNI(final String dni) {
        return (Employee) this.getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public Employee doInHibernate(final Session session) throws HibernateException, SQLException {
                Criteria idCriteria = session.createCriteria(EmployeeDAO.this.getDomainClass()).createCriteria("data");
                idCriteria.add(Restrictions.eq("id", dni));
                return (Employee) idCriteria.uniqueResult();
            }
        });
    }

}
